package adminController

import (
	"gin/chibuda/app/admin/adminService"
	"gin/chibuda/app/utils"
	"github.com/gin-gonic/gin"
	"os"
	"time"
)
//获取vue端路由
func GetRouter(c *gin.Context){
	roleId , _ :=c.Get("role_id")
	if roleId.(float64) != 1 {
		r := adminService.GetRouter(roleId)
		c.JSON( r.HttpCode, r.Rs)
	}else{
		r := adminService.GetMenu()
		c.JSON( r.HttpCode, r.Rs)
	}
}

//上传图片
func UploadFile(c *gin.Context){
	file, FormErr := c.FormFile("file")
	if FormErr!=nil{
		c.JSON( utils.SuccessCode, gin.H{"code":400,"err":"文件为空"})
		return
	}
	fileType := utils.GetBetweenStrs(file.Filename,".","")
	if fileType != ".jpg" && fileType != ".jpeg" && fileType != ".png" {
		c.JSON( utils.SuccessCode, gin.H{"code":400,"err":"文件类型不正确"})
		return
	}
	path := "/upload/"+time.Now().Format("20060102")
	Filename := utils.CreateRandomString(15) + fileType
	pathStatus := utils.CheckFileIsExist(path)
	if !pathStatus {
		CreateErr:=os.Mkdir(path,os.ModePerm)
		if CreateErr!=nil{
			c.JSON( utils.SuccessCode, gin.H{"code":402,"err":CreateErr})
			return
		}
	}
	filePath := "./public" + path + "/"+ Filename
	err := c.SaveUploadedFile(file, filePath)
	if err !=nil{
		c.JSON( utils.SuccessCode, gin.H{"code":401,"err":err})
		return
	}
	c.JSON( utils.SuccessCode, gin.H{"code":200,"filePath":filePath})
}

//获取管理员个人信息
func UserInfo(c *gin.Context){
	uid , _  := c.Get("uid")
	r := adminService.UserInfo(uid)
	c.JSON( r.HttpCode, r.Rs)
}

func ChangePsd(c *gin.Context){
	uid , _  := c.Get("uid")
	data:=utils.Input(c)
	r := adminService.ChangePsd(uid,data)
	c.JSON( r.HttpCode, r.Rs)
}

func GetIcon(c *gin.Context){
	r := adminService.GetIcon()
	c.JSON( r.HttpCode, r.Rs)
}

func Test(c *gin.Context){
	r := adminService.Test()
	c.JSON( r.HttpCode, r.Rs)
}

